package priv.pront.code.lanqiao.competition.course.date0403;

import java.util.Scanner;

public class 方格取数 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int m, n, nums;
        //将初始的数组设置为11 * 11大小，如果直接设置为N*N，会造成数组越界
        int [][] s =new int[11][11];
        int [][][][] map = new int[11][11][11][11];
        while (true){
            m = sc.nextInt();
            n = sc.nextInt();
            nums = sc.nextInt();
            //跳出循环的条件（0，0，0）
            if (m == 0 && n == 0 && nums == 0){
                break;
            }
            s[m][n] = nums;
        }
        for (int x1 = 1; x1 <= N; x1++) {
            for (int y1 = 1; y1 <= N; y1++) {
                for (int x2 = 1; x2 <= N; x2++) {
                    for (int y2 = 1; y2 <= N; y2++) {
                        map[x1][y1][x2][y2] = Math.max(Math.max(map[x1-1][y1][x2-1][y2], map[x1-1][y1][x2][y2-1]),
                                Math.max(map[x1][y1-1][x2-1][y2], map[x1][y1-1][x2][y2-1]));
                        map[x1][y1][x2][y2] += s[x1][y1];
                        //如果两次都会经过同一个点，则加一次即可，以免造成重复
                        //只要经过的点不相同，就可以直接添加
                        if (x1 != x2 && y1 != y2){
                            map[x1][y1][x2][y2] += s[x2][y2];
                        }
                    }
                }
            }
        }
        System.out.println(map[N][N][N][N]);
    }
}
